﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admin_shenhe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string usr = (string)Session["usr"];
        if (usr == null)
        {
            Response.Write("<script>alert('请返回到登录界面登录！!');window.open('login.aspx','_parent')</script>");
            Session.Clear();
        }
        //else
        //{
        //string strConnection = "Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\\web.mdb";
        //OleDbConnection objConnection = new OleDbConnection(strConnection);
        //objConnection.Open();
        //string sql = "select * from login where usr='" + usr + "'";
        //OleDbCommand cmd = new OleDbCommand(sql, objConnection);
        //OleDbDataReader reader = cmd.ExecuteReader();
        //if (reader.Read())
        //{
        //    int leader;
        //    leader = int.Parse(reader["lead"].ToString());
        //    if (leader != "")
        //    {
        //        Response.Write("<script>alert('对不起，你没有访问此页面的权限!');window.location.href ='right.aspx'</script>");
        //    }
        //    else ;
        //}

        if (!IsPostBack)
        {
            bind();
        }
    }

    public void bind()
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\\web.mdb");
        string sqlstr = "select * from news order by id desc";
        OleDbDataAdapter myda = new OleDbDataAdapter(sqlstr, con);
        DataSet myds = new DataSet();
        con.Open();
        myda.Fill(myds, "news");
        GridView1.DataSource = myds;
        GridView1.DataKeyNames = new string[] { "id" };//主键
        GridView1.DataBind();      
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(1, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\\web.mdb");
        con.Open();
        string sqlstr = "update new set timu='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',time1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',bumen='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',cishu='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',lanmu='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',,name1='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',zw='"
            + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "' where id='"
            + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        OleDbCommand sqlcom = new OleDbCommand(sqlstr, con);
        sqlcom.ExecuteNonQuery();
        con.Close();
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //GridView1.EditIndex = e.NewEditIndex;
        //bind();
        string id = GridView1.Rows[e.NewEditIndex].Cells[0].Text.ToString();
        Response.Redirect("edition.aspx?id=" + id);

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=|DataDirectory|\\web.mdb");
        con.Open();
        string sqlstr = "delete from news where id=" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "";
        OleDbCommand com = new OleDbCommand(sqlstr, con);
        com.ExecuteNonQuery();
        con.Close();
        bind();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bind();
    }
    protected void GridView1_RowDataBound2(object sender, GridViewRowEventArgs e)
    {
        int i;
        for (i = 0; i <= GridView1.Rows.Count; i++)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
    protected void gridViewPublishers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        bind();
    }
    protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
    {

    }
}